// import { koa } from "koa"
// import {koa} = require("koa")
import { bootstrapControllers } from 'koa-ts-controllers';
import * as koa from 'koa'
import * as Router from 'koa-router';
import * as bodyParser from 'koa-bodyparser';
import MainController from './controllers/main.controller'
import * as path from 'path';

const app = new koa()
const router = new Router();

(async () => { // 在controller中读取文件涉及到异步

  await bootstrapControllers(app, {
    router, // required // 内部还是要使用router来实现路由绑定
    // 定义api的规则【所有接口的基础路径】 设置访问接口路径的前缀，/api。
    basePath: '/api', 
    controllers: [  // 存放所有控制器类，是数组
      MainController, __dirname + '/controllers/**/*.ts'
    ], // It is recommended to add controller classes directly to this array, but you can also add glob strings
    // 接口版本，会附加到 basePath 之后，/api/v1。
    versions: {
      1: 'This version is deprecated and will soon be removed. Consider migrating to version 2 ASAP',
      2: true,
      dangote: true // great for custom, business client specific endpoint versions
    },
  })

  app.use(bodyParser());

  // ignore this block by setting attachRoutes: true in bootstrapControllers options
  app.use(router.routes());
  app.use(router.allowedMethods());

  // app.use(async ctx => {
  //   // ctx.body = 'hellowww'
  // })

  app.listen(8888)

})()

